#!/bin/bash

hwdetect_network() {
		# try to fix some problems with realtek network cards
		network_check_8139=$(lsmod | grep 8139cp || lsmod | grep 8139too)
		if [ -n "${network_check_8139}" ]; then

			network_8139cp=$(dmesg | grep -i 'Try the "8139too" driver instead')
			network_8139too=$(dmesg | grep -i 'Try the "8139cp" driver instead')

			if [ -n "${network_8139cp}" ]; then
				printhl3 "Detected broken realtek network driver: 8139cp"
				printhl "Trying to fix it, loading driver: 8139too"
				rmmod 8139cp &>/dev/null
				touch /etc/modprobe.d/realtek_blacklist &>/dev/null
				echo "blacklist 8139cp" >> /etc/modprobe.d/realtek_blacklist &>/dev/null
				modprobe 8139too &>/dev/null
			fi

			if [ -n "${network_8139too}" ]; then
				printhl3 "Detected broken realtek network driver: 8139too"
				printhl "Trying to fix it, loading driver: 8139cp"
				rmmod 8139too &>/dev/null
				touch /etc/modprobe.d/realtek_blacklist &>/dev/null 
				echo "blacklist 8139too" >> /etc/modprobe.d/realtek_blacklist &>/dev/null
				modprobe 8139cp &>/dev/null
			fi
		fi

		# check available network devices
		network_check=$(cat /proc/net/dev | awk '{ print  $1 }' | egrep '^(eth|ath|wlan)' | cut -d: -f1 | sed -e :a -e '$!N;s/\n/ /')	
		printhl "Available network devices: ${network_check}" 
}
